Imports System.Data
Imports System.Data.SqlClient
Imports DevExpress.XtraEditors.Controls
Imports System.IO
Imports DevExpress.XtraExport
Imports DevExpress.XtraGrid.Export

Public Class TopUpRecord

    Private Filename As String = "C:\Program Files\Common Files\MemberSystem\TopUpRecord.xml"

    Private Sub mnuSaveLayout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveLayout.Click
        Me.GridControl1.MainView.SaveLayoutToXml(Filename)
    End Sub

    Private Sub mnuLoadDefault_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuLoadDefault.Click
        Dim File, Folder, FileCollection
        'Dim fso As New Scripting.FileSystemObject

        If MsgBox("Are You Sure Want to Load Default Layout!", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then

            If IO.File.Exists(Filename) Then
                IO.File.Delete(Filename)
            End If

            MsgBox("Please Reload the Page as Default Layout Loaded", MsgBoxStyle.Information)
            Me.Close()

        End If

    End Sub

    Private Sub GridControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GridControl1.MouseDown
        If e.Button = MouseButtons.Right Then
            Me.ContextMenu = ContextMenu1
            Exit Sub
        End If
    End Sub

    Private Sub TransactionRecord_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadIcon(Me)
        loadLayout(Me.GridControl1, Filename)
        LoadMemberLevel()
        Me.txtCodeNo.Focus()
    End Sub

    Private Sub txtCodeNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCodeNo.KeyDown
        If e.KeyCode = Keys.Enter Then
            If SearchByCodeNo(Me.txtCodeNo.Text) = True Then
                Me.txtCodeNo.Text = ""
            Else

            End If
        End If
    End Sub

    Private Function SearchByCodeNo(ByVal CodeNo As String) As Boolean
        Dim Found As Boolean = False
        Dim str As String = "SELECT * FROM People WHERE CodeNo = '" & CodeNo & "'"
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader
        While dr.Read
            Me.txtName.Text = dr.Item("Name").ToString
            Me.cboMemberLevel.EditValue = dr.Item("Category")
            Me.txtCredit.EditValue = dr.Item("CreditBalance")
            Me.lblMemberID.Text = dr.Item("ID")
            Dim arrPic() As Byte
            Try
                If Not dr.Item("Picture") Is Nothing Then
                    arrPic = dr.Item("Picture")
                    Dim mmPicture As New MemoryStream(arrPic)
                    Me.pic.Image = Image.FromStream(mmPicture)
                End If
            Catch ex As Exception
            End Try
            Found = True
        End While
        dr.Close()


        'Get Records
        str = "SELECT * FROM TopUpRecords_View WHERE CardNo = '" & Me.txtCodeNo.Text & "'"
        cmd.CommandText = str
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        da.Fill(ds, "Records")
        Me.GridControl1.DataSource = ds.Tables(0)
        lblRecordsID.DataBindings.Clear()
        lblRecordsID.DataBindings.Add(New Binding("Text", ds.Tables("Records"), "ID", True))

        lblMemberID.DataBindings.Clear()
        lblMemberID.DataBindings.Add(New Binding("Text", ds.Tables("Records"), "MemberID", True))

        Me.lblDataCount.Text = GridView1.RowCount & " Records"

        Return Found
    End Function

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        If SearchByCodeNo(Me.txtCodeNo.Text) = True Then
            Me.txtCodeNo.Text = ""
        Else

        End If

    End Sub

    Private Sub LoadMemberLevel()
        'Load Category
        Dim str As String = "SELECT * FROM Category_People"
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        Me.cboMemberLevel.Properties.Columns.Clear()
        cboMemberLevel.Properties.DataSource = Nothing
        da.Fill(ds, "Category")

        cboMemberLevel.Properties.DataSource = ds.Tables("Category")
        cboMemberLevel.Properties.DisplayMember = "CategoryName"
        cboMemberLevel.Properties.ValueMember = "CategoryID"

        Dim CollCategory As DevExpress.XtraEditors.Controls.LookUpColumnInfoCollection = cboMemberLevel.Properties.Columns
        CollCategory.Add(New LookUpColumnInfo("CategoryName", "Category", 0))
        CollCategory.Add(New LookUpColumnInfo("CategoryID", "CategoryID", 0, DevExpress.Utils.FormatType.None, 0, False, DevExpress.Utils.HorzAlignment.Center))

    End Sub


    Private Sub SimpleButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton2.Click
        Me.Close()
    End Sub

    Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
        Dim saveDialog As New SaveFileDialog
        saveDialog.Title = "Export To "
        saveDialog.Filter = "Microsoft Excel|*.xls|HTML Documents|*html|Text Files|*.txt|XML Document|*.xml"
        If saveDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Try
                Select Case saveDialog.FilterIndex
                    Case 1
                        Call ExportTo(New ExportXlsProvider(saveDialog.FileName), GridView1)
                    Case 2
                        Call ExportTo(New ExportHtmlProvider(saveDialog.FileName), GridView1)
                    Case 3
                        Call ExportTo(New ExportTxtProvider(saveDialog.FileName), GridView1)
                    Case 4
                        Call ExportTo(New ExportXmlProvider(saveDialog.FileName), GridView1)
                End Select
                MsgBox("Successfully Retrive to " & saveDialog.FileName & ".", MsgBoxStyle.Information)
            Catch ex As Exception
                MsgBox("Fail To Retrive : " & ex.Message, MsgBoxStyle.Information)
            End Try

        End If
    End Sub

    Private Sub ExportTo(ByVal provider As DevExpress.XtraExport.IExportProvider, ByVal grid As DevExpress.XtraGrid.Views.Grid.GridView)
        Dim link As BaseExportLink = grid.CreateExportLink(provider)
        CType(link, GridViewExportLink).ExpandAll = False
        link.ExportTo(True)
    End Sub

    Private Sub btnReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReport.Click
        If Me.lblMemberID.Text = "" Then
            Exit Sub
        End If
        Dim frm As New Report_MemberTopUP
        frm.MemberID = Me.lblMemberID.Text
        frm.ShowDialog()
    End Sub

    Private Sub txtCodeNo_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCodeNo.EditValueChanged

    End Sub
End Class